package com.loong.musicdemo.adapter;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.SearchInfo;

import java.util.List;

/**
 * 热搜适配器
 */
public class HotSearchAdapter extends RecyclerView.Adapter<HotSearchAdapter.ViewHolder> {
    private List<SearchInfo> searchInfoList;//搜索信息列表
    private Handler handler;

    public HotSearchAdapter(List<SearchInfo> searchInfoList, Handler handler) {
        this.searchInfoList = searchInfoList;
        this.handler = handler;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
                                         int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hot_search_info_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotSearchAdapter.ViewHolder holder, int position) {
        SearchInfo searchInfo = searchInfoList.get(position);
        holder.search_content.setText(searchInfo.getContent());
        if (position == 0 || position == 1 || position == 2) {
            holder.search_order.setTextColor(Color.RED);
        }
        String value = String.valueOf(position + 1);
        holder.search_order.setText(value);
        holder.search_score.setText(searchInfo.getScore());
        holder.search_word.setText(searchInfo.getSearchWord());
        if (searchInfo.getIconUrl() != null) {
            Glide.with(holder.root.getContext()).load(searchInfo.getIconUrl()).skipMemoryCache(true).into(holder.search_icon);
        }
        holder.root.setOnClickListener(v -> {
            int pos = holder.getAdapterPosition();
            SearchInfo si = searchInfoList.get(pos);
            Message msg = Message.obtain();
            msg.what = 1;
            msg.obj = si.getSearchWord();
            handler.sendMessage(msg);//传送信息修改缓存数据
        });
    }

    @Override
    public int getItemCount() {
        return searchInfoList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout root;
        TextView search_order, search_word, search_content, search_score;
        ImageView search_icon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            search_content = itemView.findViewById(R.id.search_content);
            search_order = itemView.findViewById(R.id.search_order);
            search_word = itemView.findViewById(R.id.search_word);
            search_score = itemView.findViewById(R.id.search_score);
            search_icon = itemView.findViewById(R.id.search_icon);
        }
    }
}